/**
 * Created with IntelliJ IDEA.
 * User: Hkkkk Guo
 * Date: 2023/2/13
 * Time: 17:26
 * Description:
 */
package com.hangkai.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hangkai.reggie.common.BaseContext;
import com.hangkai.reggie.common.Result;
import com.hangkai.reggie.domain.Orders;
import com.hangkai.reggie.domain.User;
import com.hangkai.reggie.service.OrdersService;
import com.hangkai.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders){
        log.info("订单数据 {}", orders);
        ordersService.submit(orders);
        return Result.success("提交成功");
    }

    /**
     * 用户订单分页查询 打开我的界面查询订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public Result<Page<Orders>> getPage(int page, int pageSize){
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();

        Long currentUserId = BaseContext.getCurrentId();
        lqw.eq(Orders::getUserId, currentUserId);
        ordersService.page(pageInfo, lqw);
        return Result.success(pageInfo);
    }
}
